/**
 * Provides a managed resizable panel facility.
 * @namespace GW.Panel
 * @static
 * @requires todo
 * @module gwpanels
 * @author RBell 2009
 */
(function() {

YAHOO.GeoWiki.Panel = {};
var GWP = YAHOO.GeoWiki.Panel;

// Panel set overlay manager
GWP.overlayManager = new YAHOO.widget.OverlayManager();
// Default panel name lead string
GWP.setName = 'gwp-';
// Panel set index to serializes panel names, ex. gwp-0, gwp-1, ... .
GWP._panelIndex = 0;
// Default panel configuration for panels in set
GWP.panelConfig = { 
        x: 120,
        y: 120,
        width: '200px',
        height: '200px',
        visible: true,
        close: true
    };
GWP.attachID = null;

/** Create a new panel in the panel set.
 * @static
 * @method NewPanel 
 * @param {String} name The id of the panel.  If '' an unique id will be created.
 * @param {object} config Configuration parameters for this panel.  See YAHOO.widget.Panel.
 * @return {Object} The panel Object
 */
GWP.NewPanel = function( name, config ){
    // if there is no name or it is '' create one
    if( !name || name == ''){
        name = GWP.setName + GWP._panelIndex++;
    }
    // create the panel
    var tmpPanel = new YAHOO.widget.Panel( name, GWP.panelConfig );
    // merge the config with the default config
    if( config ){
        tmpPanel.cfg.applyConfig( config, true );
    }
    // register with overlay manager
    GWP.overlayManager.register([tmpPanel]);
    // force content if there is none
    if( !Boolean( tmpPanel.header )) {
        tmpPanel.setHeader( tmpPanel.id + " header " );
    };
    if( !Boolean( tmpPanel.body )) {
        tmpPanel.setBody( tmpPanel.id + " body" );
    };
    if( !Boolean( tmpPanel.footer )) {
        tmpPanel.setFooter( tmpPanel.id );
    };
    // render the panel
    if( GWP.attachID ) {
        tmpPanel.render( document.getElementById(GWP.attachID));
    } else {
        tmpPanel.render( document.body );
    };
    // add tooltip
    tmpPanel._tt_ = new YAHOO.widget.Tooltip(name+'_tt', { 
        context: name+'_h', 
        zindex: 999,
        text:"todo: tooltip text goes here" 
    });
    // setup hide/show
    tmpPanel._show = true;
    return tmpPanel;
};

/** Make panel resizable.
 * @static
 * @method Resize 
 * @param {object} panel The panel to make resizable.
 * @return {Object} The panel Object
 */
GWP.Resize = function( panel ){
    // Hold the resizeConfig in a local var since Resize dies otherwise!
    // Not sure why but it seems likely that YUI resize stores instance 
    // values in the config and this causes problems.
    var resizeConfig = {
        handles: ['br', 'tr'],
        autoRatio: false,
        minWidth: 200,
        minHeight: 100,
        status: false,
        proxy: true
    };
    var resize = new YAHOO.util.Resize( panel.id, resizeConfig );
    panel._resize_ = resize;
    // Setup startResize handler, to constrain the resize width/height
    // if the constraintoviewport configuration property is enabled.
    resize.on("startResize", function(args) {
        if (panel.cfg.getProperty("constraintoviewport")) {
            var D = YAHOO.util.Dom;
            var clientRegion = D.getClientRegion();
            var elRegion = D.getRegion(this.element);
            resize.set("maxWidth", 
                clientRegion.right - elRegion.left - 
                YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            resize.set("maxHeight", 
                clientRegion.bottom - elRegion.top - 
                YAHOO.widget.Overlay.VIEWPORT_OFFSET);
        } else {
            resize.set("maxWidth", null);
            resize.set("maxHeight", null);
        };
    }, panel, true);
    // Setup resize handler to update the Panel's 'height' to cause body to 
    // fill the new height.
    resize.on("resize", function(args) {
        var panelHeight = args.height;
        var panelWidth = args.width;
        //YAHOO.log('in _resize.on', 'dbg', 'dbg');
        if (panelHeight != 0) {
            this.cfg.setProperty("height", panelHeight + "px");
            }
        if (panelWidth != 0 ) {
            this.cfg.setProperty("width", panelWidth + "px");
            }
        // todo: fixup for a map panel, 
        if( panel._map ){       // this is a map panel
            panel._map.checkResize();
        };
    }, panel, true);
    return panel;
};

/** Show/hide the panel
 * @static
 * @method Show 
 */
GWP.Show = function( args, panel ){
    if( panel._show ){
        panel.hide();
        panel._show = false;
    } else {
        panel.show();
        panel._show = true;
    };
};

})();

